package it.rikesh.aip2011.server.service;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.slim3.datastore.Datastore;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Text;

import it.rikesh.aip2011.client.service.TrailService;
import it.rikesh.aip2011.server.meta.ActivityMeta;
import it.rikesh.aip2011.server.meta.HutMeta;
import it.rikesh.aip2011.server.meta.TrailMeta;
import it.rikesh.aip2011.shared.model.Activity;
import it.rikesh.aip2011.shared.model.FaunaFlora;
import it.rikesh.aip2011.shared.model.Hut;
import it.rikesh.aip2011.shared.model.HutTrail;
import it.rikesh.aip2011.shared.model.Trail;
import it.rikesh.aip2011.shared.model.TrailFaunaFlora;

/**
 * Classe che offre i metodi per operare sul modello Trail nel datastore Permette di eseguire query Questi metodi vengono usati dalle funzioni asincrone per permmeter alle pagine di scambiare dati con il datastore
 * @author  RN
 */
public class TrailServiceImpl implements TrailService {
    private static final Logger log = Logger.getLogger(TrailServiceImpl.class.getName());
    private TrailMeta h = new TrailMeta();
    
    public String getTrailDDescription(String id) {
        Key key=Datastore.createKey(Trail.class, Long.parseLong(id));
        Trail trail=Datastore.query(h)
            .filter(h.key.equal(key))
            .asSingle();
        return trail.getDDescription().getValue();
    }

    public List<Trail> getTrailList() {
        return Datastore.query(h).asList();
    }

    public String getTrailName(String id) {
        Key key=Datastore.createKey(Trail.class, Long.parseLong(id));
        Trail trail=Datastore.query(h)
            .filter(h.key.equal(key))
            .asSingle();
        return trail.getName();
    }

    public String setTrail(String name, String dDescription, String hut) {
        Trail tr=new Trail();
        tr.setName(name);
        tr.setDDescription(new Text(dDescription));
        HutMeta hm=HutMeta.get();
        Hut h=Datastore.query(hm).filter(hm.name.equal(hut)).asSingle();
        HutTrail ht=new HutTrail();
        ht.getHutRef().setModel(h);
        ht.getTrailRef().setModel(tr);
        Datastore.put(tr);
        Datastore.put(ht);
        return "success";
        
    }

    public List<Hut> getHutTrail(String id){
        Key key=Datastore.createKey(Trail.class, Long.parseLong(id));
        Trail trail=Datastore.query(h)
            .filter(h.key.equal(key))
            .asSingle();
        List<Hut> list=new ArrayList<Hut>();
        for (HutTrail ha : trail.getHutTrailListRef().getModelList()) {
            list.add(ha.getHutRef().getModel());
        }
        return list;
    }
    
    public List<FaunaFlora> getTrailFFauna(String id){
        Key key=Datastore.createKey(Trail.class, Long.parseLong(id));
        Trail trail=Datastore.query(h)
            .filter(h.key.equal(key))
            .asSingle();
        List<FaunaFlora> list=new ArrayList<FaunaFlora>();
        for (TrailFaunaFlora ha : trail.getTrailFaunaFloraListRef().getModelList()) {
            list.add(ha.getFaunaFloraRef().getModel());
        }
        return list;
    }
    
    public String getTrailLink(String id) {
        Key key=Datastore.createKey(Trail.class, Long.parseLong(id));
        Trail trail=Datastore.query(h)
            .filter(h.key.equal(key))
            .asSingle();
        return trail.getLink();
    }
    
    public String getTrailId(String name){
        Trail trail=Datastore.query(TrailMeta.get()).filter(TrailMeta.get().name.equal(name)).asSingle();
        log.info("Query getTrailId("+name+") eseguito");
        return String.valueOf(trail.getKey().getId());
        
    }
    
    public String getTrailActivity(String id) {
        Key key=Datastore.createKey(Trail.class, Long.parseLong(id));
        Trail trail=Datastore.query(h)
            .filter(h.key.equal(key))
            .asSingle();
        Key key2=Datastore.createKey(Activity.class, Long.parseLong(trail.getActivity()));
        Activity ac=Datastore.query(ActivityMeta.get()).filter(ActivityMeta.get().key.equal(key2)).asSingle();
        return ac.getName();
    }
}
